package com.blog.controller;

import com.alibaba.druid.util.StringUtils;
import com.blog.pojo.Blogusers;
import com.blog.pojo.PageResult;
import com.blog.service.BlogusersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: VE
 * @DATE: 2022/10/11 15:38
 * 描述:
 */
@RestController
@RequestMapping("user")
public class BloguserController {
    private Logger logger = LoggerFactory.getLogger(BloguserController.class);
    @Autowired
    private BlogusersService blogusersServicel;
    @GetMapping("test1")
    public String test1(){
        return "1111";
    }
    @GetMapping("loadUser")
    public PageResult getUserByUid(@CookieValue(required = false,value = "uid") Integer uid, HttpServletResponse response){
        if(uid==null){
            return PageResult.none("请先登录");
        }
        Blogusers blogusers = blogusersServicel.load(uid);
        return PageResult.success("成功",blogusers);
    }

    @PostMapping("register")
    public PageResult register(@RequestBody Blogusers blogusers){
        logger.info("注册的方法--手机号：" + blogusers.getUphone() + "；密码：" + blogusers.getUpass());

        //执行用户的注册方法
        boolean flag = blogusersServicel.register(blogusers);

        return PageResult.success("成功",null);

    }

    @GetMapping("login")
    public PageResult login(String uphone,String upass, HttpServletResponse rep){
        logger.info("用户登录");
        System.out.println(uphone);
        System.out.println(upass);
        if(uphone==null || StringUtils.isEmpty(uphone)){
            return PageResult.none("不能为空");
        }
        Blogusers loginUser = blogusersServicel.login(uphone);
        if(loginUser==null|| !loginUser.getUpass().equals(upass)){
            return PageResult.none("用户名或者密码错误");
        }
        Cookie cookie = new Cookie("uid",loginUser.getUid()+"");
        cookie.setMaxAge(3600);
        cookie.setPath("/");
        rep.addCookie(cookie);
        return PageResult.success("登录成功",loginUser);
    }

    @GetMapping("checkUphone")
    public PageResult checkUphone(String uphone){
        logger.info("查看手机号方法：" + uphone);
        Blogusers blogusers = blogusersServicel.checkUphone(uphone);

        if(blogusers != null){
            return PageResult.none("此手机号已经占用！");
        }
        return PageResult.success("此手机号可用",null);
    }

    @GetMapping("findBigUser")
    public PageResult findBigUser(){
        logger.info("查看博客达人");
        List<Blogusers> bigUser = blogusersServicel.findBigUser();
        System.out.println("bigUser = " + bigUser.size());

        return PageResult.success("成功",bigUser);
    }

    @GetMapping("loadFocus")
    public PageResult loadFocus(@CookieValue(required = false,value = "uid") Integer uid){
        logger.info("查看个人关注粉丝");
        int attentionNum = blogusersServicel.findAttentionNum(uid);
        int fansNum = blogusersServicel.findFansNum(uid);
        int collNum = blogusersServicel.findCollNum(uid);

        List<Blogusers> attentionList = blogusersServicel.findAttentUser(uid);
        List<Blogusers> fansList = blogusersServicel.findFansUser(uid);

        Map<String,Object> resuMap = new HashMap<>();
        resuMap.put("attentionNum",attentionNum);
        resuMap.put("fansNum",fansNum);
        resuMap.put("collNum",collNum);
        resuMap.put("attentionList",attentionList);
        resuMap.put("fansList",fansList);

        return PageResult.success("成功",resuMap);
    }
}
